package com.hbhmzy.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NovUtil {

    /**
     * 判断电话是否合法
     */
    public static boolean isMobileNO(String mobiles){
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 判断邮箱是否合法
     */
    public static boolean isEmail(String email){
        if (null==email || "".equals(email)) return false;
        //		Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
        Pattern p =  Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 以最省内存的方式读取本地资源的图片
     */
    public static Bitmap readBitMap(Context context, int resId){  
        BitmapFactory.Options opt = new BitmapFactory.Options();  
        opt.inPreferredConfig = Bitmap.Config.RGB_565;   
        opt.inPurgeable = true;  
        opt.inInputShareable = true;  
        //获取资源图片  
        InputStream is = context.getResources().openRawResource(resId);  
           return BitmapFactory.decodeStream(is,null,opt);  
  	}

 	/**
 	 * 判断通讯录面是否是第一次启动
 	 */
	public static boolean isContactsFirstStart(Context context) {
	    SharedPreferences preferences = context.getSharedPreferences(
	            "CONTACTS_TAG", 0);
	    Boolean isFirst = preferences.getBoolean("ContactsFIRSTStart", true);
	    if (isFirst) {// 第一次
	        preferences.edit().putBoolean("ContactsFIRSTStart", false).apply();
	        return true;
	    }else {
	        return false;
	    }
	}
	
	/**
     * 禁止EditText输入空格 和换行键
     */
    public static void setEditTextInhibitInputSpace(EditText editText){
        InputFilter filter=new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if(source.equals(" ")||source.toString().contentEquals("\n"))return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }

	/**
     * 禁止EditText输入特殊字符
     */
    public static void setEditTextInhibitInputSpeChat(EditText editText){

        InputFilter filter=new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String speChat="[`~!@#$%^&*()+=|{}':;,\\[\\].<>/?！￥…（）—【】‘；：”“’。，、？]";
                Pattern pattern = Pattern.compile(speChat);
                Matcher matcher = pattern.matcher(source.toString());
                if(matcher.find())return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }
	   
}
